package com.cardvalue.sys.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Fragemnt中放viewpage 的adapter
 * Created by cardvalue on 2016/3/31.
 */
public class MyFragmentPageAdapter extends FragmentPagerAdapter{
    private static final String TAG="MyFragmentPageAdapter";
    public static String[] tabTitle = { "基础资料", "第2步", "第3步", "第4步", "第5步" };	//标题
    /**
     * 待显示的Fragment的类集合，不直接存储对象是为了延迟实例化
     */
    private List<Class<? extends Fragment>> mFrmCls = new ArrayList<Class<? extends Fragment>>();
    /**
     *
     */
   public  MyFragmentPageAdapter(FragmentManager fm){
       super(fm);
    }

    /**
     * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
     */
    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        try {
            return mFrmCls.get(position).newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 向当前ViewPager添加待显示的Fragment
     *
     * @param frmCls
     *            Fragment类数组
     * */
    @SuppressWarnings("unchecked")
    public void fillFragments(Class<? extends Fragment>... frmCls) {
        if (frmCls == null || frmCls.length == 0) {
            Log.w(TAG, "未包含任何Fragment内容块");
        }
        for(Class<? extends Fragment> c : frmCls){
            mFrmCls.add(c);
        }
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mFrmCls.size();
    }

    public  int getSelectedCount(){
        return tabTitle.length;
    }
}
